package com.travel.api.controller.oss

import com.lgy.sell.controller.BaseController
import com.travel.api.service.oss.OSSService
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.util.StringUtils
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile

/**
 * @ClassName OSSController
 * @Author liguangyao
 * @Date 28/8/18 下午7:34
 * @Version 1.0
 */
@Api(value = "/oss", description = "OSS控制器")
@RestController
@RequestMapping("oss")
@CrossOrigin
class OSSController : BaseController() {

    @Autowired
    private val ossService: OSSService? = null

    @ApiOperation(value = "创建存储空间", httpMethod = "POST", notes = "创建存储空间")
    @PostMapping("/create")
    fun create(@RequestParam("bucketName") bucketName: String): Map<String, Any> {
        return if (!ossService!!.create(bucketName))
            fail("该${bucketName}空间已存在")
        else
            success()
    }

    @ApiOperation(value = "OSS文件夹创建", httpMethod = "POST", notes = "OSS文件夹创建")
    @PostMapping("/createFolder")
    fun createFolder(@RequestParam(value = "bucketName", required = false) bucketName :String?,
                     @RequestParam folderName: String): Map<String, Any> {
        return if (!ossService!!.createFolder(bucketName, folderName))
            fail("该${folderName}文件夹已存在")
        else
            success()
    }

    @ApiOperation(value = "OSS对象存储", httpMethod = "POST", notes = "OSS对象存储,bucketName默认为travelproject,若objectName已经存在则数据会覆盖")
    @PostMapping("/upload")
    fun upload(@RequestParam(value = "bucketName", required = false) bucketName: String?,
               @RequestParam objectName: String,
               @RequestParam content: String): Map<String, Any> {
        return success(null, ossService!!.upload(bucketName, objectName, content))
    }

    @ApiOperation(value = "OSS对象下载", httpMethod = "GET", notes = "OSS对象下载,bucketName默认为travelproject")
    @GetMapping("/download")
    fun download(@RequestParam(value = "bucketName", required = false) bucketName: String?,
                 @RequestParam objectName: String): Map<String, Any> {
        val content = ossService!!.download(bucketName, objectName)
        return if(StringUtils.isEmpty(content))
            fail("[OSS对象下载]该${bucketName}不存在") else success(null, content)
    }

    @ApiOperation(value = "OSS对象删除", httpMethod = "DELETE", notes = "OSS对象删除,如果bucketName不为NullobjectName为null则删除存储空间,若都不为空则删除对象")
    @DeleteMapping("/delete")
    fun delete(@RequestParam bucketName: String,@RequestParam(value = "objectName", required = false) objectName: String?): Map<String, Any> {
        return success(null, ossService!!.delete(bucketName, objectName))
    }

    @ApiOperation(value = "OSS对象列举", httpMethod = "GET", notes = "列举指定的存储空间的对象")
    @GetMapping("/list")
    fun list(@RequestParam bucketName: String): Map<String, Any> {
        return success(null, ossService!!.list(bucketName))
    }

    @ApiOperation(value = "OSS文件上传", httpMethod = "POST", notes = "OSS文件上传,bucketName默认为travelproject,")
    @PostMapping("/uploadFile")
    fun uploadFile(@RequestParam(value = "bucketName", required = false) bucketName: String?,
                   @RequestParam(value = "folder", required = false) folder: String?,
                   @RequestBody file: MultipartFile): Map<String, Any> {
        val url = ossService!!.uploadFile(bucketName, folder, file)
        val path = url?.toString()
        return if(StringUtils.isEmpty(path)) fail("文件上传失败") else success(null, url)
    }

}
